package org.zcl.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.zcl.service.MessagePublisher;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/6/22 19:14
 */
@Service
@Slf4j
public class MessagePublisherImpl implements MessagePublisher {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void leftPush(String topic, String message) {
        redisTemplate.convertAndSend(topic, message);
    }

    @Override
    public Object rightPop(String topic) {
        while (true) {
            String message = (String) redisTemplate.opsForList().rightPop(topic);
            if (StringUtils.isNotEmpty(message)) {
                return message;
            }
        }
    }
}
